<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">

    <title>商品列表</title>
    <link rel="stylesheet" href="static/css/xadmin.css" media="all">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="static/js/xadmin.js"></script>
    <script src="static/lib/layui/layui.js"></script>
    <script type="text/javascript" src="static/js/boUtil.js"></script>
    <style>
        body{margin: 10px;}
    </style>
</head>

<body class="layui-anim layui-anim-up">

<%--step.1 导航区域 begin--%>
<div class="x-nav">
    <span class="layui-breadcrumb" lay-separator="|"></span>
        <a href="">首页 > </a>
        <a href="">演示 > </a>
        <a>
          <cite>导航元素</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<%--step.1 导航区域 end--%>

<%--step.2 内容区域begin--%>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
        <input class="layui-input" placeholder="开始日" name="start" id="start">
        <input class="layui-input" placeholder="截止日" name="end" id="end">
        <input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
        <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
    </form>
    </div>
    <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('添加商品','./add-item')"><i class="layui-icon"></i>添加</button>
    </xblock>

    <table id="item-list" lay-filter="test" class="layui-table">
        <script type="text/html" id="optionBar">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="up">上架</a>
            <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="down">下架</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </table>

</div>
</body>



<%--step.2 内容区域end--%>



<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
    });

    //表头
    layui.use('table', function () {
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#item-list'
            , height: 500
            , url: 'item/list' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: 'id', title: '商品ID', sort: true}
                , {field: 'title', title: '商品标题'}
                , {field: 'cid', title: '叶子类目(所属类目)'}
                , {field: 'sellPoint', title: '卖点'}
                , {field: 'price', title: '价格', sort: true}
                , {field: 'num', title: '库存数量', sort: true}
                , {field: 'status', title: '状态'}
                , {fixed: 'right', width: 230, align: 'center', title: '操作', toolbar: '#optionBar'}
            ]]
            , even: true
            , page: true
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                const url = "/item/updateItemByStatus?itemId=" + data.id + "&status=3";
                confirm(obj,"确认删除？",url);
            } else if (obj.event === 'edit') {
                x_admin_show('添加商品', '/add-item?itemId=' + data.id)
            } else if (obj.event == 'up') {
                const url = "/item/updateItemByStatus?itemId=" + data.id + "&status=1";
                confirm(obj,"确认上架？",url);
            } else if (obj.event == 'down') {
                const url = "/item/updateItemByStatus?itemId=" + data.id + "&status=2";
                confirm(obj,"确认下架？",url);
            }


        });
        function confirm(obj,msg,url) {
            layer.confirm(msg, function (index) {
                console.log("index-->"+index)
            fetchHttp(url).then(res => {
                if (res.code === "0000" && obj.event === 'del') {
                    obj.del();
                }
            }).catch(e => {
                console.log(e)
            });
            layer.close(index);
        });
        }

    });
</script>
</body>
</html>